//
//  UIAlertController+Category.m
//  nvshengpai_2
//
//  Created by lushouxiang on 15/12/17.
//  Copyright © 2015年 nvshengpai. All rights reserved.
//

#import "UIAlertController+Category.h"


@interface UIAlertController ()<UIAlertViewDelegate>

@property (nonatomic, strong) block suerBlock;


@end


@implementation UIAlertController (Category)




#pragma mark - 将要弃用的方法

- (void)showAlert{
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:self animated:YES completion:nil];
}

+ (instancetype)alertSheet:(NSString *)title mes:(NSString *)message actionTitles:(NSArray *)titles actionBlocks:(NSArray *)blocks{
    
    UIAlertController * alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alertC dismissViewControllerAnimated:YES completion:nil];
    }];
    [alertC addAction:cancle];

    if (titles.count != blocks.count) {
        return alertC;
    }
    for (int i = 0; i < titles.count; i++) {
        UIAlertAction * action = [UIAlertAction actionWithTitle:titles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alertC dismissViewControllerAnimated:YES completion:nil];

            typedef void(^block)(UIAlertAction * _Nonnull action);
            block b = blocks[i];
            b(action);
        }];
        [alertC addAction:action];
    }
    return alertC;
}



+ (instancetype)alertSheet:(NSString *)title mes:(NSString *)message actions:(NSArray *)actions{
    
    UIAlertController * alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alertC dismissViewControllerAnimated:YES completion:nil];
    }];
    [alertC addAction:cancle];
    
    for (UIAlertAction *action in actions) {
        [alertC addAction:action];
    }
    return alertC;
}





////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma mark - 普通弹框

+ (void)showAlertMessage:(NSString *)msg AutoMiss:(BOOL)miss{
    if (miss) {
         [UIAlertController showAlertViewWithTitle:nil message:msg cancle:@"知道啦" CancleAction:nil sureTitle:nil sureAction:nil];
    }
    
}
+ (void)showSheetMessage:(NSString *)msg AutoMiss:(BOOL)miss{
    
}


+ (void)showAlertMessage:(NSString *)msg{

    [UIAlertController showAlertViewWithTitle:nil message:msg cancle:@"知道啦" CancleAction:nil sureTitle:nil sureAction:nil];
}

+ (void)showSheetMessage:(NSString *)msg{
    
    [UIAlertController showSheetViewWithTitle:nil message:msg cancle:@"知道啦" CancleAction:nil sureTitle:nil sureAction:nil];
    
}




+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancle:(NSString *)CancleTitle action:(block)cancleBlock{
    
    [UIAlertController showAlertViewWithTitle:title message:message cancle:CancleTitle CancleAction:cancleBlock sureTitle:nil sureAction:nil];
}



+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancle:(NSString *)CancleTitle sureTitle:(NSString *)sureTitle sureAction:(block)sureBlock{
    
    [UIAlertController showAlertViewWithTitle:title message:message cancle:CancleTitle CancleAction:nil sureTitle:sureTitle sureAction:sureBlock];
    
}





+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancle:(NSString *)CancleTitle CancleAction:(block)cancleBlock sureTitle:(NSString *)sureTitle sureAction:(block)sureBlock{
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    if(CancleTitle){
        
        UIAlertAction * cancle = [UIAlertAction actionWithTitle:CancleTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
            if (cancleBlock) {
                cancleBlock(action);
            }
        }];
        [alert addAction:cancle];
        
    }
    
    if (sureTitle) {
        UIAlertAction * sure = [UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
            if (sureBlock) {
                sureBlock(action);
            }
        }];
        [alert addAction:sure];
    }
    
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}









+ (void)showSheetViewWithTitle:(NSString *)title message:(NSString *)message cancle:(NSString *)CancleTitle CancleAction:(block)cancleBlock sureTitle:(NSString *)sureTitle sureAction:(block)sureBlock{
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    if(CancleTitle){
        
        UIAlertAction * cancle = [UIAlertAction actionWithTitle:CancleTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
            if (cancleBlock) {
                cancleBlock(action);
            }
        }];
        [alert addAction:cancle];
        
    }
    
    if (sureTitle) {
        UIAlertAction * sure = [UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
            if (sureBlock) {
                sureBlock(action);
            }
        }];
        [alert addAction:sure];
    }
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}





////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



#pragma mark - 多参数弹框


+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message isCancle:(NSString *)CancleTitle sureTitle:(NSString *)sureTitle suerAction:(block)suerBlock otherTitles:(NSArray *)otherTitles otherActions:(block)otherBlock,...NS_REQUIRES_NIL_TERMINATION{
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    if(CancleTitle){
        UIAlertAction * cancle = [UIAlertAction actionWithTitle:CancleTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
        }];
        [alert addAction:cancle];
    }
    if(sureTitle){
        UIAlertAction * enter = [UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
            if(suerBlock){
                suerBlock(action);
            }
        }];
        [alert addAction:enter];
    }
    
    block other = nil;
    va_list args; //用于指向第一个参数
    NSMutableArray *otherBlocks = [NSMutableArray array];
    if(otherBlock){
        
        [otherBlocks addObject:otherBlock];
        va_start(args, otherBlock);//对args进行初始化，让它指向可变参数表里面的第一个参数
        
        while((other = va_arg(args, block))){//获取指定类型的值
            [otherBlocks addObject:other];
        }
        va_end(args);//将args关闭
    }
    
    if(otherTitles.count != otherBlocks.count){
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        return;
    }
    
    for(int i = 0; i < otherTitles.count; i++ ){
        UIAlertAction * other = [UIAlertAction actionWithTitle:otherTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
            ((block)otherBlocks[i])(action);
        }];
        [alert addAction:other];
    }
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}



+ (void)showSheetViewWithTitle:(NSString *)title message:(NSString *)message isCancle:(NSString *)CancleTitle sureTitle:(NSString *)sureTitle suerAction:(block)suerBlock otherTitles:(NSArray *)otherTitles otherActions:(block)otherBlock,...NS_REQUIRES_NIL_TERMINATION{
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    if(CancleTitle){
        UIAlertAction * cancle = [UIAlertAction actionWithTitle:CancleTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
        }];
        [alert addAction:cancle];
    }
    if(sureTitle){
        UIAlertAction * enter = [UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
            if(suerBlock){
                suerBlock(action);
            }
        }];
        [alert addAction:enter];
    }
    
    block other = nil;
    va_list args; //用于指向第一个参数
    NSMutableArray *otherBlocks = [NSMutableArray array];
    if(otherBlock){
        
        [otherBlocks addObject:otherBlock];
        va_start(args, otherBlock);//对args进行初始化，让它指向可变参数表里面的第一个参数
        
        while((other = va_arg(args, block))){//获取指定类型的值
            [otherBlocks addObject:other];
        }
        va_end(args);//将args关闭
    }
    
    if(otherTitles.count != otherBlocks.count){
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        return;
    }
    
    for(int i = 0; i < otherTitles.count; i++ ){
        UIAlertAction * other = [UIAlertAction actionWithTitle:otherTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
            ((block)otherBlocks[i])(action);
        }];
        [alert addAction:other];
    }
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}





+ (void)alertShowWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle isCancle:(NSString *)CancleTitle sureTitle:(NSString *)sureTitle suerAction:(block)suerBlock otherTitles:(NSArray *)otherTitles otherActions:(block)otherBlock,...NS_REQUIRES_NIL_TERMINATION{
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
    if(CancleTitle){
        UIAlertAction * cancle = [UIAlertAction actionWithTitle:CancleTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
        }];
        [alert addAction:cancle];
    }
    if(sureTitle){
        UIAlertAction * enter = [UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
            if(suerBlock){
                suerBlock(action);
            }
        }];
        [alert addAction:enter];
    }
    
    
    block other = nil;
    va_list args; //用于指向第一个参数
    NSMutableArray *otherBlocks = [NSMutableArray array];
    if(otherBlock){
        
        [otherBlocks addObject:otherBlock];
        va_start(args, otherBlock);//对args进行初始化，让它指向可变参数表里面的第一个参数
        
        while((other = va_arg(args, block))){//获取指定类型的值
            [otherBlocks addObject:other];
        }
        va_end(args);//将args关闭
    }
    
    if(otherTitles.count != otherBlocks.count){
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];;
        return;
    }
    
    for(int i = 0; i < otherTitles.count; i++ ){
        UIAlertAction * other = [UIAlertAction actionWithTitle:otherTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
            ((block)otherBlocks[i])(action);
        }];
        [alert addAction:other];
    }
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];;
}









@end
